import pd from '/api/productData.js';
import cd from '/api/cartData.js';
import ud from '/api/usrData.js';
(() => {
    /* 加载头部导航栏 */
    $('.header-top-nav-wrap').load('/components/head-nav.html');
    /* 加载头部搜索框 并对样式进行修改 */
    $('.head').load('/components/head-search.html', function() {
        /* 隐藏位置下拉菜单 */
        $('.logo_dropdown').hide();
        /* 更改样式 */
        $('.search-bar').css('border-color', "#E12118").find('.search-btn').css('background-color', "#E12118");
        $('.search-btn').on('click', function() {
            const key = $('.search-key').val();

            // console.log(key ? "q=" + key : "title=" + $('.search-key').attr('placeholder'));
            location.href = '/pages/list.html?' + (key ? "q=" + key : "title=" + $('.search-key').attr('placeholder'));
        })
    });
    /* 加载尾部 */
    $('.bottom-footer-wrap').load('/components/bottom-footer.html');
    /* 加载右侧购物篮 */
    $('.right-bar').load('/components/right-fixed-bar.html');

    render();
})()

function render() {

    const usrData = JSON.parse(localStorage.getItem('usrData'));
    if (usrData == '{}') {
        $('noData').show().html(`
        亲还没有登录，快去<a href="/pages/login.html">登录把</a>
        `)
        return
    }
    ud.getData(`/${encodeURIComponent(usrData.token)}`, data => {
        if (JSON.stringify(data['order']) != '[]') {
            $('.noData').hide();
            let str = '';
            data['order'].forEach(o => {
                pd.getByPid(Object.keys(o.order), data => {
                    str += `
                    <div class='item'>
                        <div>
                            <p class='fl'>${o.address}</p>
                            <p class='fr'>下单时间：${dateFormat('YYYY-mm-dd HH:MM:SS',new Date(o.time))}</p>
                        </div>
                        ${
                            data.map(p => {
                                return `
                                <a href="/pages/detial?pid=${p.pid}">
                                    <img src='${p.imgsrc}' />
                                </a>
                                `;
                            })
                        }
                        <span class='fr'>
                            ￥${o.order_price}
                        </span>
                    </div>`
                    $('.order-list').html(str);
                })

            });
        }
    })
}

function dateFormat(fmt, date) {
    let ret;
    const opt = {
        "Y+": date.getFullYear().toString(), // 年
        "m+": (date.getMonth() + 1).toString(), // 月
        "d+": date.getDate().toString(), // 日
        "H+": date.getHours().toString(), // 时
        "M+": date.getMinutes().toString(), // 分
        "S+": date.getSeconds().toString() // 秒
    };
    for (let k in opt) {
        ret = new RegExp("(" + k + ")").exec(fmt);
        if (ret) {
            fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
        };
    };
    return fmt;
}